package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.dto.LoginDTO;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import java.util.Set;


@Service
public class MemberServiceImpl  implements MemberService {


    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 手机号码登录
     * @param loginDto
     * @return
     */
    public Result login(LoginDTO loginDto) {
        SetOperations<String, String> setOps = redisTemplate.opsForSet();
        // 获取验证码
        Set<String> validateCodes = setOps.members(loginDto.getTelephone());
        if (validateCodes != null && validateCodes.size() > 0){
            if (validateCodes.contains(loginDto.getValidateCode())){
                return new Result(true, MessageConstant.LOGIN_SUCCESS,null);
            }
        }
        return new Result(false,MessageConstant.VALIDATECODE_ERROR,null);
    }

}
